<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:directive.page import="java.io.File"/>
<jsp:directive.page import="java.util.List"/>
<jsp:directive.page import="java.util.Iterator"/>
<jsp:directive.page import="com.myzion.core.common.util.Constants"/>
<jsp:directive.page import="org.apache.commons.lang.StringUtils"/>
<jsp:directive.page import="org.apache.commons.fileupload.FileItem"/>
<jsp:directive.page import="org.apache.commons.fileupload.servlet.ServletFileUpload"/>
<jsp:directive.page import="org.apache.commons.fileupload.disk.DiskFileItemFactory;"/>

<%
String message = "";
String fPath = request.getParameter("filePath");
String inputFileName = request.getParameter("inputFileName");
String inputFileSize = request.getParameter("inputFileSize");
String showArea = request.getParameter("showArea");
fPath = (fPath == null ? "" : fPath);
%>
<%
String fileName = "";
if(StringUtils.isNotEmpty(request.getParameter("todo"))) {

	//上传文件的目录
    String filePath ="/upload/"+ fPath;
	String uploadPath = application.getRealPath(filePath) + "/";	
	try {
		DiskFileItemFactory factory = new DiskFileItemFactory();
		ServletFileUpload fileUpload = new ServletFileUpload(factory);
		// 设置最大文件尺寸，这里是3MB
		fileUpload.setSizeMax(3*1024*1024);
		// 设置临时目录：
		fileUpload.setHeaderEncoding("UTF-8");
		
		//得到所有的文件
		List fileItems = fileUpload.parseRequest(request);
		Iterator iter = fileItems.iterator();
		
		//如果文件目录不存在，做目录
		File modelDir = new File(uploadPath);
	    if (!modelDir.exists()) {
	        modelDir.mkdirs();
	    }
	    
	    //依次处理每一个文件
		while (iter.hasNext()) {
	FileItem item = (FileItem) iter.next();
	if (!item.isFormField()) {
		fileName = item.getName();
		long size = item.getSize();
		if (StringUtils.isEmpty(fileName) && size == 0) {
			continue;
		}
		int len = fileName.lastIndexOf("\\");
		fileName = fileName.substring(len + 1);
		

		
		File temp = new File(uploadPath + fileName);
		if(!temp.exists()) {
			item.write(temp);
		} else {
			message = "文件上传失败！ <b>["+fileName+"]</b>此文件已经存在！";
			break;
		}
	}
		}
	} catch(Exception e) {
		message = "文件上传失败，请重试上传！";
	}
  
}
%>

